<template>
  <div class="role-dialog">
    <el-dialog
      :title="isEdit ? '更新角色' : '添加角色'"
      :visible="show"
      @close="$emit('changeDialog', false)"
      width="40%"
    >
      <el-form class="custom-form" ref="form" :model="role" label-width="80px">
        <el-form-item class="custom-form-item" label="角色名称">
          <el-input v-model="role.name"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="角色编码">
          <el-input v-model="role.code"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="角色描述">
          <el-input type="textarea" v-model="role.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('changeDialog', false)">取消</el-button>
        <el-button type="primary" @click="createOrUpdate"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { createOrUpdateRole } from '@/apis/role'
export default Vue.extend({
  name: 'create-or-update-role',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    role: {
      type: Object,
      default: function() {
        return {
          id: '',
          name: '',
          code: '',
          description: ''
        }
      }
    }
  },
  methods: {
    async createOrUpdate() {
      const { data } = await createOrUpdateRole(this.role)
      if (data.code === '000000') {
        this.$message.success(this.isEdit ? '修改角色成功！' : '添加角色成功！')
        this.$emit('changeDialog', false, true)
      }
    }
  }
})
</script>
<style lang="scss" scoped>
.category-dialog {
  .custom-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .custom-form-item {
      width: 70%;
    }
  }
}
</style>
